home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 43.zip / Sources C- WorkDisk V.adf / ex / leo.c < prev    next >
C/C++ Source or Header  |  1987-02-15  |  5KB  |  234 lines

  1.  
  2.  
  3. /*
  4.  * In the tradition of stupid programs, Leo Schwab presents (yet again)
  5.  * The Art Program.
  6.  * 8511.24
  7.  */
  8.  
  9. #include <exec/types.h>
  10. #include <graphics/gfxbase.h>
  11. #include <intuition/intuition.h>
  12.  
  13. #define XSIZE          639
  14. #define YSIZE          199
  15. #define ever           (;;)
  16.  
  17. extern short     rnd ();
  18.  
  19. struct IntuitionBase   *IntuitionBase;
  20. struct GfxBase         *GfxBase;
  21. struct Screen  *scr, *OpenScreen();
  22. struct Window  *win, *OpenWindow();
  23. long GetMsg();
  24.  
  25. struct NewScreen scrdef = {
  26.     0, 0, XSIZE+1, YSIZE+1,
  27.     4,              /*  16 colors  */
  28.     0, 1,           /*  detail/block pens  */
  29.                     /*  "Automatically" set up screen format  */
  30.     ((XSIZE>320) ? HIRES : 0) | ((YSIZE>200) ? LACE : 0),
  31.     CUSTOMSCREEN,
  32.     NULL, NULL, NULL, NULL  /* no special font, title, gadg, or bitmap */
  33. };
  34.  
  35. struct NewWindow windef = {
  36.     0, 0, XSIZE+1, YSIZE+1,
  37.     0, 1,
  38.     CLOSEWINDOW,
  39.     WINDOWCLOSE | BACKDROP | BORDERLESS,
  40.     NULL, NULL, NULL,       /* no special gadget, checkmark or title */
  41.     NULL,                   /* screen pointer; will get set later */
  42.     NULL,                   /* no custom bitmap */
  43.     0, 0, 0, 0,             /* ignored */
  44.     CUSTOMSCREEN
  45. };
  46.  
  47.  
  48. main ()
  49. {
  50.     register struct RastPort *rp;
  51.     long i, dx1, dy1, dx2, dy2, pen = 0, flag = 0;
  52.     static long xa1[150], ya1[150], xa2[150], ya2[150];
  53.     register long x1, y1, x2, y2;
  54.  
  55.     openstuff ();
  56.     rnd (-87634L);
  57. /*     rp = win -> RPort;      <--- This is slower because of layers   */
  58.     rp = & (scr -> RastPort);
  59.     SetDrMd (rp, COMPLEMENT);
  60.     ShowTitle (scr, FALSE);
  61.     SetRast (rp, 0L);
  62.     x1 = rnd (XSIZE+1L);  y1 = rnd (YSIZE+1L);
  63.     x2 = rnd (XSIZE+1L);  y2 = rnd (YSIZE+1L);
  64.     setdisp (&dx1, &dy1);
  65.     setdisp (&dx2, &dy2);
  66.  
  67.     for ever {
  68.             for (i=0; i<150; i++) {
  69.                     if (GetMsg (win -> UserPort)) {
  70.                             closestuff ();
  71.                             exit (TRUE);
  72.                     }
  73.                     if (!rnd (20L))
  74.                             if (rnd (2L))
  75.                                     setdisp (&dx1, &dy1);
  76.                             else
  77.                                     setdisp (&dx2, &dy2);
  78.                     
  79.                     x1 += dx1;  y1 += dy1;
  80.                     if (x1 > XSIZE || x1 < 0) {
  81.                             dx1 = -dx1;
  82.                             x1 = x1<0 ? 0 : XSIZE;
  83.                     }
  84.                     if (y1 > YSIZE || y1 < 0) {
  85.                             dy1 = -dy1;
  86.                             y1 = y1<0 ? 0 : YSIZE;
  87.                     }
  88.  
  89.                     x2 += dx2;  y2 += dy2;
  90.                     if (x2 > XSIZE || x2 < 0) {
  91.                             dx2 = -dx2;
  92.                             x2 = x2<0 ? 0 : XSIZE;
  93.                     }
  94.                     if (y2 > YSIZE || y2 < 0) {
  95.                             dy2 = -dy2;
  96.                             y2 = y2<0 ? 0 : YSIZE;
  97.                     }
  98.  
  99.                     pen++;
  100.                     if (!(pen &= 15))
  101.                             pen++;
  102.                     rp -> Mask = pen;       /* kludge; COMPLEMENT does */
  103.                     Move (rp, x1, y1);      /* not perform as advertised */
  104.                     Draw (rp, x2, y2);
  105.                     if (flag) {
  106.                             Move (rp, xa1[i], ya1[i]);
  107.                             Draw (rp, xa2[i], ya2[i]);
  108.                     }
  109.                     xa1[i] = x1;  ya1[i] = y1;
  110.                     xa2[i] = x2;  ya2[i] = y2;
  111.             }
  112.             flag = 1;
  113.     }
  114. }
  115.  
  116. openstuff ()
  117. {
  118.     char *OpenLibrary();
  119.  
  120.     if (!(IntuitionBase = (struct IntuitionBase *)
  121.         OpenLibrary ("intuition.library", 1L))) {
  122.             printf ("Awright, where's Intuition?\n");
  123.             exit (FALSE);
  124.     }
  125.  
  126.     if (!(GfxBase = (struct GfxBase *)
  127.         OpenLibrary ("graphics.library", 1L))) {
  128.             printf ("Graphics?  Heeeere graphics....\n");
  129.             exit (FALSE);
  130.     }
  131.  
  132.     if (!(scr = OpenScreen (&scrdef))) {
  133.             printf ("Can't open your screen.\n");
  134.             exit (FALSE);
  135.     }
  136.  
  137.     windef.Screen = scr;
  138.     if (!(win = OpenWindow (&windef))) {
  139.             printf ("Window painted shut.\n");
  140.             exit (FALSE);
  141.     }
  142. }
  143.  
  144. closestuff ()
  145. {
  146.     CloseWindow (win);
  147.     CloseScreen (scr);
  148.     CloseLibrary (GfxBase);
  149.     CloseLibrary (IntuitionBase);
  150. }
  151.  
  152. /*  Commented out to test machine language equivalent
  153. short
  154. rnd (range)
  155. register int range;
  156. {
  157.     static int seed;
  158.     register int i, n;
  159.  
  160.     if (range < 0)
  161.             seed = -range;
  162.     i = (seed & 1<<27) ? 1 : 0;
  163.     n = (seed & 1<<30) ? 1 : 0;
  164.     seed <<= 1;
  165.     seed |= (i ^ n);
  166.     return ((short) ((short) seed % (short) range));
  167. }      */
  168.  
  169. setdisp (x, y)
  170. register long *x, *y;
  171. {
  172.     *x = rnd (9L) - 4;
  173.     *y = rnd (9L) - 4;
  174. }
  175.  
  176. /* ----------------------------------------------------------------------
  177. *
  178. * Here's the machine code random number generator
  179. *
  180. ------------------------------------------
  181.  
  182. *\
  183. * A random number generator.  By Leo Schwab.
  184. * Based on a hardware random number generator appearing in the TTL Cookbook
  185. * by Don Lancaster
  186. *
  187. * Calling convention:
  188. *  int rnd (range);
  189. *  int range;
  190. *
  191. * 8511.26
  192. */
  193.  
  194. #asm
  195.             public    _rnd
  196.  
  197. _rnd        move.l  d2,-(sp)
  198.             lea     rndseed,a0      ;Get address of seed
  199.             move.l  8(sp),d2        ;Get range argument
  200.             tst.l   d2
  201.             ble.s   setseed         ;Go reset seed
  202.  
  203.             move.l  d1,-(sp)        ;Probably not necessary
  204.             andi.l  #$ffff,d2       ;Coerce into a word
  205.             move.l  #$48000000,d0   ;Set bits 27 & 30
  206.             and.l   (a0),d0         ;Get interesting bits
  207.             swap    d0              ;Pull them downstairs
  208.             lsl.w   #2,d0           ;Move first bit into X flag
  209.             roxl.b  #1,d1           ; and put it in D1
  210.             rol.w   #3,d0           ;Put other bit into bit 0 of D0
  211.             eor.b   d0,d1           ;EOR them together
  212.             lsr.b   #1,d1           ;Put back into X bit
  213.             roxl.w  2(a0)           ;Generate new random number
  214.             roxl.w  (a0)
  215.             moveq   #0,d0           ;Clean D0
  216.             move.w  (a0),d0         ;Get random number
  217.             divu    d2,d0           ;Divide by range
  218.             swap    d0              ; and get remainder (modulus)
  219.             ext.l   d0
  220.             move.l  (sp)+,d1
  221.             move.l  (sp)+,d2        ;Restore D1 and D2
  222.             rts
  223.  
  224. setseed     neg.l   d2              ;Probably don't need this
  225.             move.l  d2,(a0)
  226.             move.l  (sp)+,d2
  227.             rts
  228.  
  229.  
  230.             dseg
  231. rndseed     dc.l    0
  232.             cseg
  233. #endasm
  234.